import * as Koa from 'koa';
import * as cors from 'koa2-cors'
import * as Router from 'koa-router';
import * as bodyParser from 'koa-bodyparser';
import AppRoutes from './routes';
import axios from 'axios';
const iconv = require('iconv-lite')

const config = require('./config');

const app = new Koa();
app.use(cors());
const router = new Router({
  prefix: '/api' //统一前缀
});
const port = config.SERVER_PORT || 3000;

//路由
AppRoutes.forEach(route => router[route.method](route.path, route.action));

// axios({
//   method: 'get',
//   url: 'http://yunhq.sse.com.cn:32041//v1/sh1/snap/600340?callback=jQuery1124016777756737465332_1613745752262&select=name%2Clast%2Cchg_rate%2Cchange%2Camount%2Cvolume%2Copen%2Cprev_close%2Cask%2Cbid%2Chigh%2Clow%2Ctradephase%2Chlt_tag%2Cgdr_ratio%2Cgdr_prevpx%2Cgdr_currency&_=1613745752485',
//   responseType: 'stream'
// }).then(res => {
//   const chunks = [];
//   res.data.on('data', chunk => {
//     chunks.push(chunk);
//   });
//   res.data.on('end', () => {
//     const buffer = Buffer.concat(chunks)
//     const str = iconv.decode(buffer, 'gbk')
//     console.log(str);
//   })
// })

import main from './crawler/script';
main();

app.use(bodyParser());
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(port);

console.log(`应用启动成功 端口:${port}`);
